/*
 * @Descripttion: 
 * @version: 
 * @Author: Zuo
 * @Date: 2021-09-29 14:00:07
 * @LastEditors: Zuo
 * @LastEditTime: 2021-09-29 15:57:40
 */
import axios, { AxiosResponse, AxiosRequestConfig } from "axios"
import { errorMessage } from "../common/info"
import {TIP} from '../common/tip'
import { routerReplace } from "../router/scripts/router-trigger"




let baseURL = '/api'
axios.interceptors.request.use((config:AxiosRequestConfig) => {
    
    config.headers.token = sessionStorage.getItem('token');
    return config
}, (error) => {

    // 请求错误时做些事
    return Promise.reject(error)
})

// 响应拦截器 返回状态判断
axios.interceptors.response.use((res) => {

    return res
}, (error) => {

    if (error.response.status == 401){
        errorMessage(error.response.data.err)
        setTimeout(()=>{
            routerReplace('/login')
        },2000)
    }else {
        errorMessage(TIP.SYSTEM_ERROR)
    }
    // 404等问题可以在这里处理
    return Promise.reject(error)
})


function checkStatus (response: AxiosResponse<any>) {
    // 如果http状态码正常，则直接返回数据
    if (response && (response.status === 200 || response.status === 304 ||
        response.status === 400)) {
        if (!response.data.err){
            return response.data
        }else {
            errorMessage(response.data.err)
            return response.data
        }

    }else if (response.status === 401) {
        errorMessage(response.data.err)
        setTimeout(()=>{
            routerReplace('/login')
        },2000)

    }else {
        errorMessage(TIP.SYSTEM_ERROR)
    }
}

export default {
    //formData
    post (url: any, params: any) {  //  post
        return axios({
            method: 'post',
            url: baseURL + url,
            data: params,
            transformRequest: [function (data) {
                let ret = ''
                for (let it in data) {
                    ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                }
                return ret
            }],
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }).then(
            (response) => {
                return checkStatus(response)
            }
        )
    },
    //requestBody
    postBody(url: any, params: any) {  //  post
        return axios({
            method: 'post',
            url: baseURL + url,
            data: params,
            headers: {
                'Content-Type': 'application/json',
            },
        }).then(
            (response) => {
                return checkStatus(response)
            }
        )
    },
    get (url: any, params: any) {  // get
        return axios({
            method: 'get',
            url: baseURL + url,
            params, // get 请求时带的参数
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }).then(
            (response) => {
                return checkStatus(response)
            }
        )
    },
    postQueryAndBody(url: any,params: any,datas: any){
        return axios.post(baseURL + url,params,{ params:datas }).then(
            (response) => {
                return checkStatus(response)
            })
    },
}
